﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using JetBlack.IO;

namespace JetBlack.Net.Sockets
{
    internal class FrameStream : Stream
    {
        public Stream InnerStream { get; private set; }

        public FrameStream(Stream stream)
        {
            InnerStream = stream;
        }

        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            return InnerStream.BeginRead(buffer, offset, count, callback, state);
        }

        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            return InnerStream.BeginWrite(buffer, offset, count, callback, state);
        }

        public override bool CanRead
        {
            get { return InnerStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return InnerStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return InnerStream.CanWrite; }
        }

        public override bool CanTimeout
        {
            get { return InnerStream.CanTimeout; }
        }

        public override void Close()
        {
            InnerStream.Close();
        }

        public override int EndRead(IAsyncResult asyncResult)
        {
            return InnerStream.EndRead(asyncResult);
        }

        public override void EndWrite(IAsyncResult asyncResult)
        {
            InnerStream.EndWrite(asyncResult);
        }

        public override void Flush()
        {
            InnerStream.Flush();
        }

        public override long Length
        {
            get { return InnerStream.Length; }
        }

        public override long Position
        {
            get
            {
                return InnerStream.Position;
            }
            set
            {
                InnerStream.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return InnerStream.Read(buffer, offset, count);
        }

        public override int ReadTimeout
        {
            get { return InnerStream.ReadTimeout; }
            set { InnerStream.ReadTimeout = value; }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return InnerStream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            InnerStream.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            InnerStream.Write(buffer, offset, count);
        }

        public byte[] ReadFrame()
        {
            byte[] len = new byte[4];
            InnerStream.Read(len, 0, 4);
            byte[] buffer = new byte[NetworkBitConverter.ToInt32(len, 0)];
            InnerStream.Read(buffer, 0, buffer.Length);
            return buffer;
        }

        public void WriteFrame(byte[] buffer)
        {
            InnerStream.Write(buffer.Length);
            InnerStream.Write(buffer, 0, buffer.Length);
        }

        public override int WriteTimeout
        {
            get { return InnerStream.WriteTimeout; }
            set { InnerStream.WriteTimeout = value; }
        }

        public override System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)
        {
            return InnerStream.CreateObjRef(requestedType);
        }

        public override int ReadByte()
        {
            return InnerStream.ReadByte();
        }

        public override void WriteByte(byte value)
        {
            InnerStream.WriteByte(value);
        }

        public override string ToString()
        {
            return InnerStream.ToString();
        }
    }
}
